package com.jn.erp.web.api.course.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.utils.DateUtils;
import com.jn.erp.web.api.common.enums.ErrorEnum;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.TCourseOrderConfig;
import com.jn.erp.web.api.course.entity.TCourseOrderDetail;
import com.jn.erp.web.api.course.entity.dto.StudentOrderListExcal2CDto;
import com.jn.erp.web.api.course.entity.dto.StudentOrderListExcal2SDto;
import com.jn.erp.web.api.course.entity.dto.StudentOrderListExcalDto;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.entity.vo.CourseTempVo;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderConfigVo;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderVo;
import com.jn.erp.web.api.course.mapper.TCourseOrderConfigMapper;
import com.jn.erp.web.api.course.mapper.TCourseOrderDetailMapper;
import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.course.task.TCourseOrderTask;
import com.jn.erp.web.api.interaction.entity.TEvaluate;
import com.jn.erp.web.api.interaction.entity.TIntegral;
import com.jn.erp.web.api.interaction.entity.TIntegralConfig;
import com.jn.erp.web.api.interaction.mapper.TEvaluateMapper;
import com.jn.erp.web.api.interaction.mapper.TIntegralConfigMapper;
import com.jn.erp.web.api.interaction.mapper.TIntegralMapper;
import com.jn.erp.web.api.student.entity.Student;
import com.jn.erp.web.api.student.entity.StudentCourse;
import com.jn.erp.web.api.student.mapper.StudentMapper;
import com.jn.erp.web.api.student.service.StudentCourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TCourseOrderServiceImpl extends ServiceImpl<TCourseOrderMapper, TCourseOrder> implements TCourseOrderService {

    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;
    @Autowired
    private TCourseOrderConfigMapper tCourseOrderConfigMapper;
    @Autowired
    private StudentCourseService studentCourseService;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TEvaluateMapper tEvaluateMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TCourseOrderTask tCourseOrderTask;

    @Autowired
    private TIntegralConfigMapper tIntegralConfigMapper;

    @Autowired
    private TIntegralMapper tIntegralMapper;

    @Autowired
    private TCourseOrderDetailMapper tCourseOrderDetailMapper;

    @Override
    public TCourseOrder lastCourseOrder(LocalDateTime startTime, Long cofId,Long studentId) {
        return this.baseMapper.lastCourseOrder( startTime,cofId,studentId);
    }

    @Override
    public List<TCourseOrderDto> courseOrderList(TCourseOrderVo tCourseOrderVo) {
        if(StringUtils.isNotBlank(tCourseOrderVo.getOnlyThisWeek()) && "0".compareTo(tCourseOrderVo.getOnlyThisWeek()) == 0){
            tCourseOrderVo.setWeekStartTime(DateUtils.getMon().toString());
            tCourseOrderVo.setWeekEndTime(DateUtils.getMon().plusDays(6).toString());
        }

        List<TCourseOrderDto> tCourseOrderDtos = tCourseOrderMapper.courseOrderList(tCourseOrderVo);

        // 判断过没过期
        // 点没点完名
        // 试听过这节课的不能再试听

//        if(StringUtils.isNotBlank(tCourseOrderVo.getOnlyThisWeek()) && "0".compareTo(tCourseOrderVo.getOnlyThisWeek()) == 0){
//            tCourseOrderDtos = tCourseOrderDtos.stream().sorted(Comparator.comparing(TCourseOrderDto::getOrderNo)).collect(Collectors.toList());
//        }
        return tCourseOrderDtos;
    }

    @Override
    public List<StudentOrderListExcalDto> exportOrderList(TCourseOrderVo tCourseOrderVo) {
        List<StudentOrderListExcalDto> studentOrderListExcalDtos = tCourseOrderMapper.exportOrderList(tCourseOrderVo);
        return studentOrderListExcalDtos;
    }

    @Override
    public TCourseOrderDto getCourseOrderById(TCourseOrderVo tCourseOrderVo) {
        TCourseOrderDto  tCourseOrderDto = baseMapper.getCourseOrderById(tCourseOrderVo);
        if(!Objects.isNull(tCourseOrderDto) && !StringUtils.isEmpty(tCourseOrderDto.getAssistant())){
            String[] split = tCourseOrderDto.getAssistant().split(",");
            if(split.length > 0){
                List<Integer> collect = Arrays.stream(split).map(u -> {
                    return Integer.parseInt(u);
                }).collect(Collectors.toList());
                tCourseOrderDto.setAssistantArr(collect);
            }
        }

        return tCourseOrderDto;
    }

    @Override
    public List<TCourseOrderDto> orderStudentList(TCourseOrderVo tCourseOrderVo) {
        List<TCourseOrderDto> tCourseOrderDtos = baseMapper.orderStudentList(tCourseOrderVo);
        return tCourseOrderDtos;
    }

    @Override
    public List<TCourseOrderDto> orderListSecond(TCourseOrderVo tCourseOrderVo) {
        List<TCourseOrderDto> tCourseOrderDtos = baseMapper.orderListSecond(tCourseOrderVo);

        return tCourseOrderDtos;
    }

    @Override
    public String addStudent(TCourseOrderVo tCourseOrderVo) {
        List<CourseTempVo> ids = tCourseOrderVo.getIds();
        TCourseOrderConfig tCourseOrderConfig = tCourseOrderConfigMapper.selectById(tCourseOrderVo.getCofId());
        List<TCourseOrder> tCourseOrders = new ArrayList<>();
        ids.stream().forEach(u->{
            TCourseOrder courseOrder = new TCourseOrder(tCourseOrderConfig.getOrgId(), tCourseOrderConfig.getId(),tCourseOrderVo.getOrderNo(), 1, u.getCourseId(), tCourseOrderConfig.getClassId()
                    , tCourseOrderVo.getClassDate(), tCourseOrderConfig.getClassStartTime(), tCourseOrderConfig.getClassEndTime(), tCourseOrderConfig.getWeekNum(), u.getId()
                    , tCourseOrderConfig.getTeacher(), tCourseOrderConfig.getAssistant(), tCourseOrderConfig.getClassRoom(),tCourseOrderConfig.getDescription(), "1", tCourseOrderConfig.getClassHour(), 0, null, null
                    ,tCourseOrderVo.getTemp(),tCourseOrderVo.getTempOrderNo() ,LocalDateTime.now(), "sys", LocalDateTime.now(), "sys", "0");
            courseOrder.setType(1);
            if (tCourseOrderVo.getClassDates() != null) {
                LocalDateTime localDateTime = DateUtils.getLocalDateTime(tCourseOrderVo.getClassDates() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
                courseOrder.setClassDate(localDateTime);
            }

            tCourseOrders.add(courseOrder);
        });
        this.saveBatch(tCourseOrders);
        return "S200";
    }

    @Override
    public List<StudentOrderListExcal2CDto> exportOrderListSecondC(TCourseOrderVo tCourseOrderVo) {
        List<StudentOrderListExcal2CDto> studentOrderListExcal2CDtos = tCourseOrderMapper.exportOrderListSecondC(tCourseOrderVo);
        return studentOrderListExcal2CDtos;
    }

    @Override
    public List<StudentOrderListExcal2SDto> exportOrderListSecondS(TCourseOrderVo tCourseOrderVo) {
        List<StudentOrderListExcal2SDto> studentOrderListExcal2SDtos = tCourseOrderMapper.exportOrderListSecondS(tCourseOrderVo);
        return studentOrderListExcal2SDtos;
    }

    @Override
    public String updateBatchOrder(List<TCourseOrder> tCourseOrders) {

//        boolean b = this.updateBatchById(tCourseOrders);
        for(TCourseOrder u :tCourseOrders){
            Student s = studentMapper.selectById(u.getStudentId());
            s.setStage(2);
            // 试听点名状态变成 已体验
            u.setType(u.getState());
            List<StudentCourse> studentCourses = studentCourseService.list(new QueryWrapper<StudentCourse>()
                    .eq("deleted","0").eq("exhausted",1).eq("student_id",u.getStudentId()).eq("course_id",u.getCourseId()));
//            点名迟到变更为在读，更改消费表的数据，
            if(!Objects.isNull(u.getClassHourPay())&& u.getClassHourPay()>0){
                studentMapper.update(s,new QueryWrapper<Student>().eq("id",u.getStudentId()));
              //                studentCourses =  studentCourses.stream().filter(i->i.getCountLessonComplete()<(i.getCountLessonTotal()-i.getCountLessonRefund())).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(studentCourses)  ){
                    TCourseOrder old = baseMapper.selectById(u.getId());
                    if(!Objects.isNull(old) && !Objects.isNull(old.getClassHourPay()) && 0 != old.getClassHourPay()){
                        StudentCourse studentCourse = studentCourseService.getById(old.getTscId());
                        studentCourse.setCountLessonComplete(studentCourse.getCountLessonComplete()-old.getClassHourPay());
                        studentCourseService.updateById(studentCourse);
                    }
                }else if(3 != u.getTemp()){
                    throw new RuntimeException( ErrorEnum.E103.getName()) ;
                }
                dealStudentCourse(u,studentCourses,"点名扣除");
            }else{
                TCourseOrder old = baseMapper.selectById(u.getId());
                if(!Objects.isNull(old)  && !Objects.isNull(old.getClassHourPay())  && 0 != old.getClassHourPay()){
                    StudentCourse studentCourse = studentCourseService.getById(old.getTscId());
                    studentCourse.setCountLessonComplete(studentCourse.getCountLessonComplete()-old.getClassHourPay());
                    studentCourseService.updateById(studentCourse);
                }
                dealStudentCourse(u,studentCourses,"点名扣除");
            }
            //添加评价信息
            TEvaluate tEvaluate = new TEvaluate(u.getOrgId(),u.getStudentId(),u.getOrderNo(),1,s.getName(),1,u.getState(),null,null,null,null,null,null,LocalDateTime.now(),u.getUpdateUser(),null,null,"0");
            QueryWrapper<TEvaluate> eq = new QueryWrapper<TEvaluate>().eq("org_id", tEvaluate.getOrgId()).eq("order_no", tEvaluate.getOrderNo()).eq("t_id", tEvaluate.getTId()).eq("is_delete", "0");
            List<TEvaluate> tEvaluates = tEvaluateMapper.selectList(eq);
            if(CollectionUtils.isEmpty(tEvaluates)){
                tEvaluateMapper.insert(tEvaluate);
            }else{
                tEvaluate.setState(null);
                tEvaluate.setCreateUser(null);
                tEvaluate.setCreateDate(null);
                tEvaluate.setUpdateUser(u.getUpdateUser());
                tEvaluate.setUpdateDate(LocalDateTime.now());
                tEvaluateMapper.update(tEvaluate,eq);
            }
            //添加积分
            TIntegralConfig t = tIntegralConfigMapper.selectOne(new QueryWrapper<TIntegralConfig>().eq("org_id", u.getOrgId()).eq("is_delete","0"));
            if(!Objects.isNull(t)&&(u.getState() ==2||u.getState() ==3)){
                TIntegral tIntegral = new TIntegral(u.getOrgId(),u.getStudentId(),t.getId(),2,t.getAttr2(),t.getName(),"点名",LocalDateTime.now(),u.getUpdateUser() ,null,null,"0");
                tIntegralMapper.insert(tIntegral);
            }
        }
        return "S200";
    }

    @Override
    public String updateOrderById(TCourseOrder tCourseOrder) {
        TCourseOrder old = baseMapper.selectById(tCourseOrder.getId());
        //标记已补直接处理，修改课次（上课记录）要进行课时处理
        if(!Objects.isNull(tCourseOrder.getState()) ){
            if(3 != old.getTemp()){
                StudentCourse studentCourse = studentCourseService.getById(old.getTscId());
                if(studentCourse.getExhausted() == 0){
                    throw new RuntimeException(ErrorEnum.E109.getName());
                }
                Integer countLessonComplete = 0;
                if(Objects.isNull(studentCourse)){
                    studentCourse= new StudentCourse();
                    countLessonComplete = 0;
                }else{
                    countLessonComplete = studentCourse.getCountLessonComplete() - old.getClassHourPay();
                }
                studentCourse.setCountLessonComplete(countLessonComplete);
                studentCourse.setEditor(Long.valueOf(tCourseOrder.getUpdateUser()));
                studentCourse.setEditTime(LocalDateTime.now());
                studentCourseService.updateById(studentCourse);
                List<StudentCourse> studentCourses = studentCourseService.list(new QueryWrapper<StudentCourse>()
                        .eq("deleted","0").eq("exhausted",1).eq("student_id",old.getStudentId()).eq("course_id",old.getCourseId()));
                TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(old.getOrgId(),old.getOrderNo(),studentCourse.getOrderNo(),2,-old.getClassHourPay(),-old.getClassHourPayNormal(),old.getClassHourPayNormal() -old.getClassHourPay(),"修改点名回退",LocalDateTime.now(),tCourseOrder.getUpdateUser(),null,null,"0");
                tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                int i = baseMapper.updateById(tCourseOrder);
                TCourseOrder n = baseMapper.selectById(tCourseOrder.getId());
                n.setClassHourPayNormal(0);
                // 试听学员状态
                n.setType(tCourseOrder.getState());
                dealStudentCourse(n,studentCourses,"修改点名扣除");
            }else{
                if(tCourseOrder.getClassHourPay() != 0 ){
                    throw new RuntimeException(ErrorEnum.E108.getName()) ;
                }

                int i = baseMapper.updateById(tCourseOrder);
            }
            //积分处理
            if(old.getState()==2||old.getState()==3 ){
                if(tCourseOrder.getState()==4||tCourseOrder.getState()==5){
                    //添加积分
                    TIntegralConfig t = tIntegralConfigMapper.selectOne(new QueryWrapper<TIntegralConfig>().eq("org_id", old.getOrgId()).eq("is_delete","0"));
                    if(!Objects.isNull(t)){
                        TIntegral tIntegral = new TIntegral(old.getOrgId(),old.getStudentId(),t.getId(),2,-t.getAttr2(),t.getName(),"修改点名回退",LocalDateTime.now(),String.valueOf(tCourseOrder.getUpdateUser()) ,null,null,"0");
                        tIntegralMapper.insert(tIntegral);
                    }
                }
            }else{
                if(tCourseOrder.getState()==2||tCourseOrder.getState()==3){
                    //添加积分
                    TIntegralConfig t = tIntegralConfigMapper.selectOne(new QueryWrapper<TIntegralConfig>().eq("org_id", old.getOrgId()).eq("is_delete","0"));
                    if(!Objects.isNull(t)){
                        TIntegral tIntegral = new TIntegral(old.getOrgId(),old.getStudentId(),t.getId(),2,t.getAttr2(),t.getName(),"修改点名",LocalDateTime.now(),String.valueOf(tCourseOrder.getUpdateUser()) ,null,null,"0");
                        tIntegralMapper.insert(tIntegral);
                    }
                }
            }
        }else{
            int i = baseMapper.updateById(tCourseOrder);
        }

        return ErrorEnum.S200.getName();
    }

    private void dealStudentCourse(TCourseOrder t ,List<StudentCourse> studentCourses,String source){

        Integer temp =t.getClassHourPay();
        Boolean flag = true;
        for( int i = 0 ; i < studentCourses.size() ; i++){
            StudentCourse s = studentCourses.get(i);
//                扣除课时类型0正常课时1赠送课时2复合扣课时

            if (temp <= s.getCountLessonTotalNormal() - s.getCountLessonComplete()) {
                s.setCountLessonComplete(s.getCountLessonComplete() + temp);

                studentCourseService.updateById(s);
                t.setTscId(s.getId());
                t.setClassHourPayNormal(temp);
                t.setClassHourPayType("0");
                flag = false;
                TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,temp,0,source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                break;
            } else if ( s.getCountLessonTotalNormal() - s.getCountLessonComplete() >0 && temp <= s.getCountLessonTotal() - s.getCountLessonComplete()) {
                Integer classHourPayNormal = s.getCountLessonTotalNormal() - s.getCountLessonComplete();
                t.setClassHourPayNormal(classHourPayNormal);
                s.setCountLessonComplete(s.getCountLessonComplete() + temp);
                studentCourseService.updateById(s);
                t.setTscId(s.getId());
                t.setClassHourPayType("2");
                flag = false;
                TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,classHourPayNormal,temp - classHourPayNormal,source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                break;
            } else if (temp <= s.getCountLessonTotal() - s.getCountLessonComplete()) {
                s.setCountLessonComplete(s.getCountLessonComplete() + temp);
                studentCourseService.updateById(s);
                t.setTscId(s.getId());
                t.setClassHourPayType("1");
                flag = false;
                TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,0,temp,source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                break;
            }else  if(i == studentCourses.size() -1) {
                if(s.getCountLessonTotalNormal() - s.getCountLessonComplete() >0){
                    Integer classHourPayNormal = temp - s.getCountLessonTotalGive();
                    s.setCountLessonComplete(s.getCountLessonComplete() + temp);
                    studentCourseService.updateById(s);
                    t.setTscId(s.getId());
                    t.setClassHourPayNormal(classHourPayNormal);
                    t.setClassHourPayType("3");
                    flag = false;
                    TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,classHourPayNormal,s.getCountLessonTotalGive(),source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                    tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                }else if(s.getCountLessonTotal() - s.getCountLessonComplete() >0){
                    Integer classHourPayNormal = temp  - (s.getCountLessonTotal() - s.getCountLessonComplete());
                    s.setCountLessonComplete(s.getCountLessonComplete() + temp);
                    studentCourseService.updateById(s);
                    t.setTscId(s.getId());
                    t.setClassHourPayNormal(classHourPayNormal);
                    t.setClassHourPayType("4");
                    flag = false;
                    TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,classHourPayNormal,s.getCountLessonTotal() - s.getCountLessonComplete(),source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                    tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                }else{
                    Integer classHourPayNormal = temp;
                    s.setCountLessonComplete(s.getCountLessonComplete() + temp);
                    studentCourseService.updateById(s);
                    t.setTscId(s.getId());
                    t.setClassHourPayNormal(classHourPayNormal);
                    t.setClassHourPayType("5");
                    flag = false;
                    TCourseOrderDetail tCourseOrderDetail = new TCourseOrderDetail(s.getOrgId(),t.getOrderNo(),s.getOrderNo(),1,temp,classHourPayNormal,0,source,LocalDateTime.now(),t.getUpdateUser(),null,null,"0");
                    tCourseOrderDetailMapper.insert(tCourseOrderDetail);
                }
                break;
            }
        }

        if(flag && 3 != t.getTemp()){
            throw new RuntimeException(ErrorEnum.E107.getName());
        }

        boolean b = this.updateById(t);
    }
    @Override
    public String saveRepair(TCourseOrderConfigVo tCourseOrderConfigVo) {
        //可能存在Redis容灾不生效的的情况取数据库数据进行初始化
        tCourseOrderTask.initOrderNo();

        tCourseOrderConfigVo.setType(2);
        tCourseOrderConfigVo.setState(1);
        tCourseOrderConfigMapper.insert(tCourseOrderConfigVo);
        List<TCourseOrder> tCourseOrders = tCourseOrderConfigVo.gettCourseOrders();
        List<TCourseOrder> list = new ArrayList<>();
        tCourseOrders.stream().forEach(u->{
            Student s = studentMapper.selectById(u.getStudentId());
//            LocalDateTime classDate = DateUtils.getLocalDateTime(tCourseOrderConfigVo.getClassDate(), "yyyy-MM-dd'T'HH:mm:ss.SSS");
            LocalDateTime classDate = DateUtils.getLocalDateTime(tCourseOrderConfigVo.getClassDate(), "yyyy-MM-dd HH:mm:ss");
            String orderNo =tCourseOrderTask.getOrderNo(classDate,"courseOrderNo",tCourseOrderConfigVo.getId());
            u.setCreateDate(LocalDateTime.now());
            u = new TCourseOrder(tCourseOrderConfigVo.getOrgId(), tCourseOrderConfigVo.getId(),orderNo, u.getState(), tCourseOrderConfigVo.getCourseId(), tCourseOrderConfigVo.getClassId()
                    , classDate, tCourseOrderConfigVo.getClassStartTime(), tCourseOrderConfigVo.getClassEndTime(), tCourseOrderConfigVo.getWeekNum(), u.getStudentId()
                    , tCourseOrderConfigVo.getTeacher(), tCourseOrderConfigVo.getAssistant(), tCourseOrderConfigVo.getClassRoom(),tCourseOrderConfigVo.getDescription(), "1", tCourseOrderConfigVo.getClassHour(), u.getClassHourPay(), LocalDateTime.now(), u.getRemark()
                    ,2,u.getTempOrderNo() ,LocalDateTime.now(), "sys", LocalDateTime.now(), "sys", "0");
            list.add(u);
           //处理学生学时
            List<StudentCourse> studentCourses = studentCourseService.list(new QueryWrapper<StudentCourse>()
                    .eq("deleted","0").eq("exhausted",1).eq("student_id",u.getStudentId()).eq("course_id",u.getCourseId()));
            if(!CollectionUtils.isEmpty(studentCourses)){
                dealStudentCourse(u,studentCourses,"点名扣除-补课");
            }else{
                throw new RuntimeException( ErrorEnum.E103.getName()) ;
            }
            //添加评价信息
            TEvaluate tEvaluate = new TEvaluate(u.getOrgId(),u.getStudentId(),u.getOrderNo(),1,s.getName(),1,u.getState(),null,null,null,null,null,null,LocalDateTime.now(),u.getUpdateUser(),null,null,"0");
            tEvaluateMapper.insert(tEvaluate);
        });
        this.saveBatch(list);

        return "S200";
    }

    @Override
    public List<TCourseOrderDto> repairDetail(TCourseOrderVo tCourseOrderVo) {
        List<TCourseOrderDto> tCourseOrders = baseMapper.repairDetail(tCourseOrderVo);
        return tCourseOrders;
    }

    @Override
    public List<TCourseOrderDto> orderListThird(TCourseOrderVo tCourseOrderVo) {
        List<TCourseOrderDto> tCourseOrderDtos = baseMapper.orderListThird(tCourseOrderVo);
        return tCourseOrderDtos;
    }

    @Override
    public TCourseOrderDto orderDetailSecond(TCourseOrderVo tCourseOrderVo) {
        TCourseOrderDto tCourseOrderDto = baseMapper.orderDetailSecond(tCourseOrderVo);
        return tCourseOrderDto;
    }

    @Override
    public Map orderCountSecond(TCourseOrderVo tCourseOrderVo) {
        Map map = baseMapper.orderCountSecond(tCourseOrderVo);
        return map;
    }

    @Override
    public int called(String orderNo) {
        int i = baseMapper.called(orderNo);
        return i;
    }

    @Override
    public List<TCourseOrderDto> orderFilterList(TCourseOrderVo tCourseOrderVo) {
        if(StringUtils.isNotBlank(tCourseOrderVo.getOnlyThisWeek()) && "0".compareTo(tCourseOrderVo.getOnlyThisWeek()) == 0){
            tCourseOrderVo.setWeekStartTime(DateUtils.getMon().toString());
            tCourseOrderVo.setWeekEndTime(DateUtils.getMon().plusDays(6).toString());
        }
        List<TCourseOrderDto> tCourseOrderDtos = tCourseOrderMapper.courseOrderLists(tCourseOrderVo);
        if(StringUtils.isNotBlank(tCourseOrderVo.getOnlyThisWeek()) && "0".compareTo(tCourseOrderVo.getOnlyThisWeek()) == 0){
            tCourseOrderDtos = tCourseOrderDtos.stream().sorted(Comparator.comparing(TCourseOrderDto::getOrderNo)).collect(Collectors.toList());
        }
        return tCourseOrderDtos;
    }

    @Override
    public String getMaxOrderNo(String date) {
        String orderNo = baseMapper.getMaxOrderNo(date);
        return orderNo;
    }
}
